package com.wx.soft.contoller.wx;

import com.wx.soft.contoller.common.BaseController;
import com.wx.sys.model.SysUser;
import com.wx.sys.model.WxAccount;
import com.wx.sys.service.WxAccountFeignApi;
import com.wx.util.IdTool;
import com.wx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by king-pc on 2018/12/6.
 */
@Controller
@RequestMapping("/account")
public class WxAccountController extends BaseController {

    @Autowired
    private WxAccountFeignApi wxAccountFeignApi;

    /**
     * 添加
     * @return
     */
    @GetMapping("/add")
    public String add(HttpServletRequest request,Model model){
        String userId = this.getCurUserId(request);
        WxAccount account = wxAccountFeignApi.getWxAccountByUserId(userId);
        if(account==null){
            return "/sys/accountAdd";
        }
        model.addAttribute("account",account);
        return "/sys/accountView";
    }

    /**
     *添加微信公众账号
     * @param request
     * @param wxAccount
     * @return
     */
    @ResponseBody
    @PostMapping("/addAccount")
    public String addAccount(HttpServletRequest request,WxAccount wxAccount){
        String userId = this.getCurUserId(request);
        wxAccount.setUrl("/wxIndex/"+wxAccount.getAccount()+"/replyMsg");
        wxAccount.setToken(IdTool.getId());
        wxAccount.setUserId(userId);
        wxAccount.setId(IdTool.getId());
        wxAccountFeignApi.addAccount(wxAccount);
        return R.ok();
    }
}
